package com.project.voicememoassistant.service.impl;

import com.project.voicememoassistant.mapper.*;
import com.project.voicememoassistant.model.*;
import com.project.voicememoassistant.service.FindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class FindServiceImpl implements FindService {

    @Resource
    private LedgerMapper ledgerMapper;
    @Resource
    private MenstruationMapper menstruationMapper;
    @Resource
    private PermanentMapper permanentMapper;
    @Resource
    private RemindMapper remindMapper;
    @Resource
    private ScheduleMapper scheduleMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TipsMapper tipsMapper;

    @Override
    public Map<String,Object> findAllLeagerByUid(String uid) {
        LedgerExample ledgerExample = new LedgerExample();
        ledgerExample.createCriteria().andUidEqualTo(uid);
        List<Ledger> list = ledgerMapper.selectByExample(ledgerExample);
        List<Ledger> listType1 = new ArrayList<>();
        List<Ledger> listType2 = new ArrayList<>();
        List<Ledger> listType3 = new ArrayList<>();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        for(int i=0;i<list.size();i++){
            Date date = new Date(list.get(i).getDate());
            String time = dateformat.format(date);
            list.get(i).setDate(time);
            if(list.get(i).getEventtype() == 1){
                listType1.add(list.get(i));
            }else if(list.get(i).getEventtype() == 2){
                listType2.add(list.get(i));
            }else if(list.get(i).getEventtype() == 3){
                listType3.add(list.get(i));
            }
        }
        Map map = new HashMap();
        map.put("type1",listType1);
        map.put("type2",listType2);
        map.put("type3",listType3);
        return map;
    }

    @Override
    public List<Menstruation> findAllMenstruationByUid(String uid) {
        MenstruationExample menstruationExample = new MenstruationExample();
        menstruationExample.createCriteria().andUidEqualTo(uid);
        List<Menstruation> list = menstruationMapper.selectByExample(menstruationExample);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        for(int i=0;i<list.size();i++){
            Date date = new Date(list.get(i).getPeriod());
            String time = dateformat.format(date);
            list.get(i).setPeriod(time);
        }
        return list;
    }

    @Override
    public List<Permanent> findAllPermanentByUid(String uid) {
        PermanentExample permanentExample = new PermanentExample();
        permanentExample.createCriteria().andUidEqualTo(uid);
        List<Permanent> list = permanentMapper.selectByExample(permanentExample);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        for(int i=0;i<list.size();i++){
            Date date = new Date(list.get(i).getTime());
            String time = dateformat.format(date);
            list.get(i).setTime(time);
        }
        return list;
    }

    @Override
    public List<Permanent> findPermanentByType(String uid,int type) {
        List<Permanent> listToReturn = new ArrayList<>();
        PermanentExample permanentExample = new PermanentExample();
        permanentExample.createCriteria().andUidEqualTo(uid);
        List<Permanent> list = permanentMapper.selectByExample(permanentExample);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        for(int i=0;i<list.size();i++){
            Date date = new Date(list.get(i).getTime());
            String time = dateformat.format(date);
            list.get(i).setTime(time);
            if(list.get(i).getType() == type)
                listToReturn.add(list.get(i));
        }
        return listToReturn;
    }

    @Override
    public List<Remind> findRemindByType(String uid,int type) {
        List<Remind> listToReturn = new ArrayList<>();
        RemindExample remindExample = new RemindExample();
        remindExample.createCriteria().andUidEqualTo(uid);
        List<Remind> list = remindMapper.selectByExample(remindExample);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        for(int i=0;i<list.size();i++){
            Date date = new Date(list.get(i).getTime());
            String time = dateformat.format(date);
            list.get(i).setTime(time);
            if(list.get(i).getRemindtype() == type)
                listToReturn.add(list.get(i));
        }
        return listToReturn;
    }

    @Override
    public List<Remind> findAllRemindByUid(String uid) {
        RemindExample remindExample = new RemindExample();
        remindExample.createCriteria().andUidEqualTo(uid);
        List<Remind> list = remindMapper.selectByExample(remindExample);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        for(int i=0;i<list.size();i++){
            Date date = new Date(list.get(i).getTime());
            String time = dateformat.format(date);
            list.get(i).setTime(time);
        }
        return list;
    }

    @Override
    public List<Remind> findAllRemindDate(String uid, String date) {
        RemindExample remindExample = new RemindExample();
        remindExample.createCriteria().andUidEqualTo(uid);
        List<Remind> list = remindMapper.selectByExample(remindExample);
        List<Remind> listToReturn = new ArrayList<>();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        Date date1;
        for(int i=0;i<list.size();i++){
            date1 = new Date(list.get(i).getTime());
            String time = dateformat.format(date1);
            list.get(i).setTime(time);
            if(date.equals(time.substring(0,10))){
                listToReturn.add(list.get(i));
            }
        }
        return listToReturn;
    }

    @Override
    public List<Schedule> findAllScheduleByUid(String uid) {
        ScheduleExample scheduleExample = new ScheduleExample();
        scheduleExample.createCriteria().andUidEqualTo(uid);
        List<Schedule> list = scheduleMapper.selectByExample(scheduleExample);
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
        for(int i=0;i<list.size();i++){
            Date date = new Date(list.get(i).getTime());
            String time = dateformat.format(date);
            list.get(i).setTime(time);
        }
        return list;
    }

    @Override
    public List<User> findAllUser() {
        UserExample userExample = new UserExample();
        List<User> list = userMapper.selectByExample(userExample);
        return list;
    }

    @Override
    public List<Remind> findRemindByUidInSomeTimes(String uid) {
        try{
            RemindExample remindExample = new RemindExample();
            remindExample.createCriteria().andUidEqualTo(uid);
            List<Remind> list = remindMapper.selectByExample(remindExample);
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat dateFormatChina = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
            List<Remind> listIn15Min = new ArrayList<>();
            for(int i=0;i<list.size();i++){
                Date date = new Date(list.get(i).getTime());
                String remindTime = dateformat.format(date).substring(0,16);
                String currentTime = dateFormatChina.format(System.currentTimeMillis());
                String futureTime = getDate(currentTime,15,1).substring(0,16);
                if(futureTime.equals(remindTime)){
                    list.get(i).setTime(dateformat.format(date).substring(11));
                    listIn15Min.add(list.get(i));
                }
            }
            if(listIn15Min!=null)
                return listIn15Min;
            else return new ArrayList<>();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Menstruation> findAllMenstruationByUidInSomeTimes(String uid) {
        try{
            MenstruationExample menstruationExample = new MenstruationExample();
            menstruationExample.createCriteria().andUidEqualTo(uid);
            List<Menstruation> list = menstruationMapper.selectByExample(menstruationExample);
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat dateFormatChina = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            dateformat.setTimeZone(TimeZone.getTimeZone("CST"));
            List<Menstruation> listIn15Min = new ArrayList<>();
            for(int i=0;i<list.size();i++){
                Date date = new Date(list.get(i).getPeriod());
                String menstruationTime = dateformat.format(date);
                String currentTime = dateFormatChina.format(System.currentTimeMillis());
                Date date1 = dateformat.parse(currentTime);
                menstruationTime = getDate(menstruationTime,24*27*((int)getMonthDiff(date,date1)+1),2).substring(0,10);
                String futureTime = getDate(currentTime,72,2).substring(0,10);
                if(futureTime.equals(menstruationTime)){
                    listIn15Min.add(list.get(i));
                }
            }
            if(listIn15Min!=null)
                return listIn15Min;
            else
                return new ArrayList<>();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String findATip() {
        TipsExample tipsExample = new TipsExample();
        tipsExample.setOrderByClause("rand() limit 1");
        String tip = tipsMapper.selectByExample(tipsExample).get(0).getTip();
        return tip;
    }

    @Override
    public void updateToken(String token) {
        Tips tips = new Tips();
        tips.setId(999);
        tips.setTip(token);
        tipsMapper.updateByPrimaryKey(tips);
    }

    @Override
    public String findToken() {
        return tipsMapper.selectByPrimaryKey(999).getTip();
    }

    public static String getDate(String s,int num,int type) throws ParseException {
        //给定模式(这里给定的模式须与给定日期字符串格式匹配)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(s);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);//设置起时间
        if(type == 1)
            cal.add(Calendar.MINUTE,num);
        else if(type == 2)
            cal.add(Calendar.HOUR,num);
        String dateStr = sdf.format(cal.getTime());
        return dateStr;
    }

    public static long getMonthDiff(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        int year1 = c1.get(Calendar.YEAR);
        int year2 = c2.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);
        // 获取年的差值
        int yearInterval = year1 - year2;
        // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
        if (month1 < month2 || month1 == month2 && day1 < day2) {
            yearInterval--;
        }
        // 获取月数差值
        int monthInterval = (month1 + 12) - month2;
        if (day1 < day2) {
            monthInterval--;
        }
        monthInterval %= 12;
        int monthsDiff = Math.abs(yearInterval * 12 + monthInterval)-1;
        return monthsDiff;
    }

}
